<?php
namespace app\finance\admin;
use app\system\admin\Admin;
use app\finance\model\Account as AccountModel;
use think\Db;
class Account extends Admin
{
    protected $hisiModel = 'Account';//模型名称[通用添加、修改专用]
    protected $hisiTable = '';//表名称[通用添加、修改专用]
    protected $hisiAddScene = '';//添加数据验证场景名
    protected $hisiEditScene = '';//更新数据验证场景名

    public function index()
    {
        if ($this->request->isAjax()) {

            $where      = $data = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $keyword    = $this->request->param('keyword/s');

            if ($keyword) {
                $where[] = ['name', 'like', '%'.$keyword.'%'];
            }

            $data['data']   = AccountModel::where($where)->page($page)->limit($limit)->order('weigh asc')->select();
            foreach ($data['data'] as $key => $value) {
                $data['data'][$key]['in'] = Db::name('system_balance_log')->where('account_id',$value['id'])->where('cate',1)->where('is_effective',1)->sum('price');
                $data['data'][$key]['out'] = Db::name('system_balance_log')->where('account_id',$value['id'])->where('cate',2)->where('is_effective',1)->sum('price');
                $data['data'][$key]['balance'] = bcsub($data['data'][$key]['in'],$data['data'][$key]['out'],2);
            }
            $data['count']  = AccountModel::where($where)->count('id');
            $data['code']   = 0;
            return json($data);

        }
        return $this->fetch();
    }
}
